﻿Imports DTO
Imports System.Data.Linq

Public Class PhieuTraDao
    Public Shared Function LayBang() As Table(Of PhieuTraSach)
        Dim db As New ThuVienDataContext()
        Return db.PhieuTraSaches
    End Function

    Public Shared Function LayDanhSach() As IEnumerable(Of PhieuTraSach)
        Dim lst As IEnumerable(Of PhieuTraSach)
        Dim db As New ThuVienDataContext()
        lst = From pt In db.PhieuTraSaches Select pt
        Return lst
    End Function

    Public Shared Function LayPhieuTra(ByVal mt As Integer) As PhieuTraSach
        Dim db As New ThuVienDataContext()
        Dim lst = From p In db.PhieuTraSaches Where p.MT = mt Select p
        Dim phieu As New PhieuTraSach

        If lst.Count > 0 Then
            phieu = lst.Last
        Else
            phieu.MPT = -1
        End If

        Return phieu
    End Function

    Public Shared Sub Them(ByVal t As PhieuTraSach, ByVal m As PhieuMuonSach)
        Dim db As New ThuVienDataContext()
        db.PhieuTraSaches.InsertOnSubmit(t)
        db.SubmitChanges()
        Dim chitiet = (From ct In db.ChiTietMuonTras Where ct.MPM = m.MPM Select ct).Single
        chitiet.MPT = t.MPT
        ChiTietMuonTraDao.Capnhat(chitiet)
    End Sub

    'Public Shared Sub ThemChiTiet(ByVal m As PhieuTraSach, ByVal t As ChiTietMuonTra)
    '    Dim db As New ThuVienDataContext()
    '    db.PhieuTraSaches.InsertOnSubmit(m)
    '    db.SubmitChanges()
    '    t.MPT = m.MPT
    '    db.ChiTietMuonTras.InsertOnSubmit(t)
    '    db.SubmitChanges()
    'End Sub
End Class
